package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.ISignBizService;
import com.kk.xhr.admin.biz.IWorkTimeBizService;
import com.kk.xhr.admin.controller.request.ApplyQueryRequest;
import com.kk.xhr.admin.controller.request.ApplyUpdateRequest;
import com.kk.xhr.admin.controller.request.ComplementSignAddRequest;
import com.kk.xhr.admin.controller.request.SignQueryRequest;
import com.kk.xhr.admin.controller.request.WorkTimeAddRequest;
import com.kk.xhr.admin.controller.vo.ComplementSignVO;
import com.kk.xhr.admin.controller.vo.SignVo;
import com.kk.xhr.admin.controller.vo.WorkTimeVO;
import com.kk.xhr.util.DateUtil;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 考勤管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/17
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "考勤管理", tags = "考勤管理")
@RequestMapping("/checkWork")
public class CheckWorkController {
    private final Executor adminControllerBizExecutor;
    private final ISignBizService signBizService;
    private final IWorkTimeBizService workTimeBizService;

    @ApiOperation(value = "签到")
    @GetMapping("/sign")
    public CompletionStage<ApiResult<Void>> sign() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            signBizService.sign(userId, DateUtil.now());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的考勤签到信息")
    @GetMapping
    public CompletionStage<ApiResult<PageVO<SignVo>>> queryMine(@Valid SignQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(signBizService.queryPage(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的补签申请", notes = "时间降序排序")
    @GetMapping("/complementSign")
    public CompletionStage<ApiResult<PageVO<ComplementSignVO>>> queryMineComplementSign(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(signBizService.queryPage(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "补签申请")
    @PostMapping("/complementSign")
    public CompletionStage<ApiResult<Void>> complementSign(@RequestBody @Valid ComplementSignAddRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            signBizService.complementSign(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "审核申请")
    @PutMapping("/check")
    public CompletionStage<ApiResult<Void>> checkApply(@RequestBody @Valid ApplyUpdateRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            signBizService.checkApply(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询审核补签", notes = "时间降序排序")
    @GetMapping("/checkComplement")
    public CompletionStage<ApiResult<PageVO<ComplementSignVO>>> checkComplement(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(signBizService.queryPageCheck(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询上班时间")
    @GetMapping("/workTime")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<WorkTimeVO>> queryWorkTime() {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(workTimeBizService.query())
            , adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改上班时间")
    @PutMapping("/workTime/{id}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyWorkTime(@PathVariable("id") Integer id, @RequestBody @Valid WorkTimeAddRequest workTime) {
        return CompletableFuture.supplyAsync(() -> {
            workTimeBizService.modify(id, workTime);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
